<?php

class ACLCore {

    private $_username;
    private $_role;
    private $_ci;
    private $_permitedViews = array();
    private $_rsacl = array();
    private $_staticACL;

    public function __construct($config=array()) {
        $this->_ci = &get_instance();
        $this->_ci->load->library('session');
        $this->_ci->load->model('module_model');
        $this->_rsacl = $this->_ci->config->item('rsacl');
        $this->_staticACL = $this->_ci->config->item('static_acl');
        //$this->_role = $this->_ci->session->userdata('RolId');
    }

    public function getUserRole() {
        return $this->_role;
    }

    public function hasPermission($controller, $action) {
        if ($this->_staticACL)
            return $this->permited($controller, $action, $this->_role);
        else
            return $this->_ci->module_model->permited($controller, $action, $this->_role);
    }

    public function permited($controller, $action, $id_perfil) {
        $res = false;
        foreach ($this->_rsacl as $aclPerfil) {
            if ($aclPerfil['id_perfil'] === $id_perfil) {
                foreach ($aclPerfil['acl']['modules'] as $module) {
                    if ($module['name'] === $controller) {
                        if ($module['access'] === "open") {
                            $res = true;
                            break;
                        } elseif ($module['access'] === "restricted") {
                            if (in_array($action, $module['pages'])) {
                                $res = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return $res;
    }

    public function getPermitedViews($type=null) {
        $views = array();
        $controllerName = "";
        $controller = "";
        $actionName = "";
        $this->_permitedViews = array();
        if ($this->_staticACL) {
            foreach ($this->_rsacl as $aclPerfil) {
                if ($aclPerfil['id_perfil'] === $this->_role) {
                    foreach ($aclPerfil['acl']['modules'] as $module) {
                        $controller = $module['name'];
                        $controllerName = $module['menu_name'];
                        //solo creamos un menu si posee la pagina index
                        if(in_array('index',$module['pages'])){
                            $this->_permitedViews[] = array(
                                'controllerid' => $controller,
                                'controllername' => $controllerName,
                                'controller' => $controller,
                                'page_name' => $controllerName,
                                'action' => 'index'
                            );
                        }
                    }
                }
            }
        } else {
            /*
            if (!empty($this->_username)) {
                $results = $this->_ci->module_model->getPermitedViews($type, $this->_role);
                foreach ($results as $row) {
                    $this->_permitedViews[] = array(
                        'controllerid' => $row->controllerid,
                        'controllername' => $row->controllername,
                        'controller' => $row->controller,
                        'page_name' => utf8_decode($row->Description),
                        'action' => $row->action
                    );
                }
            } else {
                $this->_permitedViews = array();
            }
             *
             */
            if (!empty($this->_username)) {
                /*if($this->_ci->session->userdata('logged_in')){
                    $this->_permitedViews = $this->_ci->session->userdata('permited_views');
                }
                else{*/
                    $this->_permitedViews = $this->_ci->module_model->getPermitedViews($type, $this->_role);
                //}
            } else {
                $this->_permitedViews = array();
            }
        }
        return $this->_permitedViews;
    }
    public function setUsername($username) {
        $this->_username = $username;
    }

    public function setUserRol($perfil) {
        $this->_role = $perfil;
    }

}

